package study.ct.com.rxjavaapplication;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import study.ct.com.rxjavaapplication.MainFragment.FragmentHome;
import study.ct.com.rxjavaapplication.MainFragment.FragmentTrip;
import study.ct.com.rxjavaapplication.MainFragment.FragmentWeather;
import study.ct.com.rxjavaapplication.bin.TripRes;
import study.ct.com.rxjavaapplication.service.CtService;

public class MainActivity extends AppCompatActivity {

    private ViewPager content_viewPager;
    private static final String TAG = "MainActivity";
    private List<Fragment> fragments = new ArrayList<>();
    private FragmentPagerAdapter pagerAdapter;


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    content_viewPager.setCurrentItem(0);

                    return true;
                case R.id.navigation_dashboard:
                    content_viewPager.setCurrentItem(1);

                    return true;
                case R.id.navigation_notifications:
                    content_viewPager.setCurrentItem(2);
                    return true;
            }
            return false;
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        FragmentManager fragmentManager=getSupportFragmentManager();
        pagerAdapter=new FragmentPagerAdapter(fragmentManager) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };

        content_viewPager = (ViewPager) findViewById(R.id.content);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        initFragment();
        content_viewPager.setAdapter(pagerAdapter);

    }

    Fragment fragment_home;
    Fragment fragment_trip;
    Fragment fragment_weather;
    public void initFragment(){
        fragment_home=new FragmentHome();
        fragment_trip=new FragmentTrip();
        fragment_weather=new FragmentWeather();
        fragments.add(fragment_home);
        fragments.add(fragment_trip);
        fragments.add(fragment_weather);
    }


    @Override
    protected void onStart() {
        super.onStart();

       // createObserverDemo();
       // createObserverTest();

    }





    //Rxjava  test
    private void createObserverTest() {
    /*
     * Observer有三个回调方法：
     *onNext：接受到一个事件
     *onCompleted：接受完事件后调用，只会调用一次
     *onError ：发生错误时调用，并停止接受事件，调用一次
     *注：onCompleted和onError不会同时调用，只会调用其中之一
     * */
        Observable<String> mObservable = Observable.create(new ObservableOnSubscribe<String>() {

            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {

                Log.i(TAG, "subscribe:  Thread id is:" + Thread.currentThread().getName());
                //emitter.onNext(getreso);

            }
        });


    }

    // RxJava create observer demo
    private void createObserverDemo() {

         /*
         * Observer有三个回调方法：
         *onNext：接受到一个事件
         *onCompleted：接受完事件后调用，只会调用一次
         *onError ：发生错误时调用，并停止接受事件，调用一次
         *注：onCompleted和onError不会同时调用，只会调用其中之一
         * */
        Observable<String> mObservable = Observable.create(new ObservableOnSubscribe<String>() {

            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {

                emitter.onNext("I am");
                emitter.onNext("RxJava");
                emitter.onNext("example");
                emitter.onNext(new Throwable("error").toString());
                emitter.onComplete();
            }
        });

        Observer<String> observer = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.e(TAG, "onSubscribe: ===========" + d.toString());
            }

            @Override
            public void onNext(String s) {
                Log.e(TAG, "onNext: s==" + s);
            }

            @Override
            public void onError(Throwable e) {
                Log.e(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                Log.i(TAG, "onComplete: ======");
            }
        };

        //注册观察者
        mObservable.subscribe(observer);
    }


    public void getResponse(){
        String url = "http://www.baidu.com";
        //OkHttpClient
    }

}
